package cn.com.chec.en.controller.admin;

import cn.com.chec.en.dao.WebsiteInfoDao;
import cn.com.chec.en.model.domain.WebsiteInfo;
import cn.com.chec.en.util.PageBean;
import cn.com.chec.en.util.WebUtils;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class WebsiteInfoController {
	protected static final Logger logger = Logger
			.getLogger(WebsiteInfoController.class);

	@Resource
	private WebsiteInfoDao websiteInfoDao;

	@RequestMapping(value = { "/admin/websiteInfo/list.do" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET })
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/websiteInfo/websiteInfoList");
		int curPage = WebUtils.getRequestParameterAsInt(request, "curPage", 1);
		String kw = WebUtils.getRequestParameterAsString(request, "kw");

		PageBean pageBean = new PageBean();
		pageBean.setCurPage(curPage);
		List<WebsiteInfo> websiteList = this.websiteInfoDao.getSubList(kw,
				pageBean.getStart(), pageBean.getRowsPerPage());
		pageBean.setMaxRowCount(this.websiteInfoDao.getCount(kw));
		pageBean.setMaxPage();
		pageBean.setPageNoList();

		map.put("websiteList", websiteList);
		map.put("pageBean", pageBean);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/admin/websiteInfo/showUpdate.do" })
	public ModelAndView showUpdate(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		ModelAndView mav = new ModelAndView("admin/websiteInfo/websiteInfoOpr");
		int id = WebUtils.getRequestParameterAsInt(request, "id", 0);
		WebsiteInfo website = this.websiteInfoDao.getWebsite(id);
		map.put("act", "update");
		map.put("website", website);
		mav.addAllObjects(map);
		return mav;
	}

	@RequestMapping({ "/admin/websiteInfo/update.do" })
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response, HttpSession session, ModelMap map)
			throws Exception {
		String message = "";
		ModelAndView mav = new ModelAndView("admin/common/message");
		int id = WebUtils.getRequestParameterAsInt(request, "id", 0);
		String name = WebUtils.getRequestParameterAsString(request, "name");
		String desc = WebUtils.getRequestParameterAsString(request, "desc");
		String sign = WebUtils.getRequestParameterAsString(request, "sign");

		WebsiteInfo website = this.websiteInfoDao.getWebsite(id);
		website.setName(name);
		website.setDesc(desc);
		website.setSign(sign);

		boolean flag = this.websiteInfoDao.update(website);
		if (flag)
			message = "分站信息更新完毕,请进行分站信息同步!";
		else {
			message = "分站信息更新失败!";
		}
		map.put("message", message);
		mav.addAllObjects(map);
		return mav;
	}
}